package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.turbulent.text.SuperText;
	import ca.turbulent.media.Pyro;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import ca.turbulent.media.events.PyroEvent;
	import ca.turbulent.media.events.StatusUpdateEvent;
	
	public class EmbedWelcome extends NFBPlayerView
	{
		
		public var label				:SuperText = new SuperText(512, 61, new TextFormat(ComModule.getInstance().core.mainFont.fontName, 18, 0xF69400, null, null, null, null, null, null, null, null, null, 2));
		public function EmbedWelcome()
		{
			super();
		}
		
		override protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();	
			this.visible = false;
			ComModule.getInstance().addEventListener(ComModule.FILE_URL_RECEIVED_EVENT, fileURLReceived, false, 0, true);
			var button:MovieClip = playButton_mc;	
			this.useHandCursor = true;
			this.mouseEnabled = true;
			this.buttonMode = true;
			this.addEventListener(MouseEvent.CLICK, clicked, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_OUT, mOut, false, 0, true);
			this.addEventListener(MouseEvent.MOUSE_OVER, mOver, false, 0, true);
			
			core.screenReaderManager.addAccess(button, "Close", "Fermer"); 	
			
			label.x = 128;
			label.y = 11;
			
			label.alignLeft();
			label.autoSize = false;
			label.multiline = true;
			label.wordWrap = true;
			label.interactive = false;
			
			label.autoRedrawBackground = true;
			label.backgroundType = SuperText.BACKGROUND_ADVANCED;
			label.backgroundOutlineAlpha = 0;
			label.hasBackground = true;
			label.backgroundAlpha = 0;
			label.backgroundColor = 0x000000;
			label.backgroundOutlineColor = 0xFD6400;
			label.backgroundOutlineThickness = 0;
			label.antiAliasType = SuperText.ANTI_ALIAS_ADVANCED;
			label.thickness = 100;
			label.sharpness = -20;
			
			addChild(label);
			positionUI();
		}
		
		private function clicked(evt:MouseEvent):void
		{
			this.visible = false;
			core.setCurrentState(NFBPlayer.DEFAULT_STATE);	
			player.play(ComModule.getInstance().latestFileURL);
		}
		
		private function mOut(evt:MouseEvent):void
		{
			playButton_mc.alpha = .5;
		}
		
		private function mOver(evt:MouseEvent):void
		{
			playButton_mc.alpha = 1;
		}
		
		private function fileURLReceived(evt:Event):void
		{
			playButton_mc.alpha = .5;
			var videoInfos:Object = ComModule.getInstance().currentMovieInfos;
			label.htmlText = videoInfos.title + "<br>"+videoInfos.director;
			label.getTextAsset().setTextFormat(new TextFormat(ComModule.getInstance().core.mainFont.fontName, 14, 0xFFFFFF, null, null, null, null, null, null, null, null, null, 2), String(videoInfos.title).length, label.getTextAsset().text.length);
		}
		
		public function show():void
		{
			positionUI();
			if (label.getTextAsset().length > 0)
			{
				var videoInfos:Object = ComModule.getInstance().currentMovieInfos;
				label.getTextAsset().setTextFormat(new TextFormat(ComModule.getInstance().core.mainFont.fontName, 14, 0xFFFFFF, null, null, null, null, null, null, null, null, null, 2), String(videoInfos.title).length, label.getTextAsset().text.length);
			}
			
			this.visible = true;
			this.tabIndex = 1;
			this.tabEnabled = true;
			var screenReaderString:String = label.getTextAsset().text;
			core.screenReaderManager.addAccess(this, screenReaderString, screenReaderString);
		} 
		
		public function hide():void
		{
			this.visible = false;
		}
		
		
		public function positionUI():void
		{
			MovieClip(background_mc).width = stage.stageWidth-(core.outlineThickness*2);
			background_mc.x = core.outlineThickness;
			MovieClip(playButton_mc).x = stage.stageWidth - 59;
			label.resize(playButton_mc.x - label.x, 61);
			
		}
		
		override protected function fullscreenHandler(evt:FullScreenEvent):void 
		{
			positionUI();
			// label.getTextAsset().setTextFormat(new TextFormat(ComModule.getInstance().core.mainFont.fontName, 14, 0xFFFFFF, null, null, null, null, null, null, null, null, null, 2), String(ComModule.getInstance().currentMovieInfos.title).length, label.getTextAsset().text.length);
		}
		
		override protected function stageResized(evt:Event=null):void
		{
			positionUI();
		}
		
		
		override protected function streamStarted(evt:PyroEvent):void 
		{ 
			hide();
		}
		
		override protected function coreStateUpdated(evt:Event=null):void
		{
			switch (core.currentState)
			{
				case NFBPlayer.DEFAULT_STATE:
				case NFBPlayer.PREVIEW_IMAGE_STATE:
				break
				
				default:
				hide();
				break;
			}
		}
	
		override protected function streamStatusUpdated(evt:StatusUpdateEvent):void 
		{ 
			switch(evt.status)
			{
				case Pyro.STATUS_PLAYING:
				hide();
				break;
			}
		}
		
	}
}